package com.qiuling.iot.utils;

import com.qiuling.iot.Exception.code.BaseResponseCode;
import com.qiuling.iot.Exception.code.ResponseCodeInterface;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 2:53 2020/2/8
 * @Modified By：
 */
@Data
public class DataResult<T> {

    /**
     * 请求响应code， 0表示请求成功 其它表示失败
     */
    @ApiModelProperty(value = "请求响应code，0为成功 其他为失败")
    private int code;

    /**
     * 响应客户端的提示
     */
    @ApiModelProperty(value = "响应异常码详细信息")
    private String msg;

    /**
     * 响应客户端内容
     */
    @ApiModelProperty(value = "响应客户端内容")
    private T data;

    public DataResult(int code, T data) {
        this.code = code;
        this.data = data;
        this.msg = null;
    }

    public DataResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public DataResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public DataResult() {
        this.code = BaseResponseCode.SUCCESS.getCode();
        this.msg = BaseResponseCode.SUCCESS.getMsg();
        this.data = null;
    }

    public DataResult(T data) {
        this.data = data;
        this.code = BaseResponseCode.SUCCESS.getCode();
        this.msg = BaseResponseCode.SUCCESS.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface) {
        this.data = null;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }

    public DataResult(ResponseCodeInterface responseCodeInterface, T data) {
        this.data = data;
        this.code = responseCodeInterface.getCode();
        this.msg = responseCodeInterface.getMsg();
    }

    /**
     * @param
     * @return com.qiuling.iot.utils.DataResult
     * @description 操作成功 data为null
     * <br/>
     * @author zhangqiang
     * @date 2:54 2020/2/8
     */
    public static <T> DataResult success() {
        return new <T>DataResult();
    }

    /**
     * @param data
     * @return com.qiuling.iot.utils.DataResult
     * @description 操作成功，返回data
     * </br>
     * @author zhangqiang
     * @date 2:55 2020/2/8
     */
    public static <T> DataResult success(T data) {
        return new <T>DataResult(data);
    }

    /**
     * @param code
     * @param msg
     * @param data
     * @return com.qiuling.iot.utils.DataResult
     * @description 自定义 返回操作 data 可控
     * </br>
     * @author zhangqiang
     * @date 2:55 2020/2/8
     */
    public static <T> DataResult getResult(int code, String msg, T data) {
        return new <T>DataResult(code, msg, data);
    }

    /**
     * @param code
     * @param msg
     * @return com.qiuling.iot.utils.DataResult
     * @description 自定义返回空数据
     * </br>
     * @author zhangqiang
     * @date 2:56 2020/2/8
     */
    public static <T> DataResult getResult(int code, String msg) {
        return new <T>DataResult(code, msg);
    }

    /**
     * @param responseCode
     * @return com.qiuling.iot.utils.DataResult
     * @description 自定义返回 入参一般是异常code枚举 data为空
     * </br>
     * @author zhangqiang
     * @date 2:56 2020/2/8
     */
    public static <T> DataResult getResult(BaseResponseCode responseCode) {
        return new <T>DataResult(responseCode);
    }

    /**
     * @param responseCode
     * @param data
     * @return com.qiuling.iot.utils.DataResult
     * @description 自定义返回 入参一般是异常code枚举 data可控
     * </br>
     * @author zhangqiang
     * @date 2:57 2020/2/8
     */
    public static <T> DataResult getResult(BaseResponseCode responseCode, T data) {

        return new <T>DataResult(responseCode, data);
    }
}
